/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *              C E D A R
 *          S O L U T I O N S       "Software done right."
 *           S O F T W A R E
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Copyright (c) 2011 Kenneth J. Pronovici.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Apache License, Version 2.0.
 * See doc/LICENSE for more information about the licensing terms.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * Author   : Kenneth J. Pronovici <pronovic@ieee.org>
 * Language : Java 6
 * Project  : Secret Santa Exchange
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package com.cedarsolutions.santa.client.root.presenter;

import static com.cedarsolutions.santa.client.SantaExchangeEventTypes.ACCOUNT_LOCKED_PAGE;

import com.cedarsolutions.client.gwt.rpc.IGaeUserRpcAsync;
import com.cedarsolutions.santa.client.common.widget.WidgetUtils;
import com.cedarsolutions.santa.client.root.RootEventBus;
import com.cedarsolutions.santa.client.rpc.util.AsyncRpcCallback;
import com.cedarsolutions.santa.shared.domain.ClientSession;
import com.cedarsolutions.shared.domain.OpenIdProvider;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.EventHandler;
import com.mvp4g.client.event.BaseEventHandler;

/**
 * Event handler to deal with login-related events.
 * @author Kenneth J. Pronovici <pronovic@ieee.org>
 */
@EventHandler
public class LoginEventHandler extends BaseEventHandler<RootEventBus> {

    /** Client session used by this module. */
    @Inject private ClientSession session;

    /** GAE user service. */
    @Inject private IGaeUserRpcAsync gaeUserRpc;

    /**
     * Show the login page for a particular destination token.
     * @param openIdProvider   Open ID provider that will be used
     * @param destinationToken Destination token (page) to redirect to after login
     */
    public void onShowLoginPageForToken(OpenIdProvider openIdProvider, String destinationToken) {
        String destinationUrl = WidgetUtils.getInstance().getDestinationUrl(destinationToken);
        this.onShowLoginPageForUrl(openIdProvider, destinationUrl);
    }

    /**
     * Show the login page for a particular destination URL.
     * @param openIdProvider   Open ID provider that will be used
     * @param destinationUrl   Destination URL to redirect to after login
     */
    public void onShowLoginPageForUrl(OpenIdProvider openIdProvider, String destinationUrl) {
        WidgetUtils.getInstance().showPleaseWaitProgressIndicator();
        GetLoginUrlCallback callback = new GetLoginUrlCallback(this.eventBus);
        this.gaeUserRpc.getLoginUrl(openIdProvider, destinationUrl, callback);
    }

    /** Log out the current user. */
    public void onLogout() {
        if (this.session.getLogoutUrl() != null) {
            // If we have a logout URL, clear the session before redirecting over to it
            String logoutUrl = this.session.getLogoutUrl();
            this.eventBus.clearSession();
            WidgetUtils.getInstance().redirect(logoutUrl);
        } else {
            // If we can't do anything else, clear the session and dump them at the landing page
            this.eventBus.clearSession();
            this.eventBus.showLandingPage();
        }
    }

    /** Lock the user out of the application. */
    public void onLockOutUser() {
        String destinationUrl = WidgetUtils.getInstance().getDestinationUrl(ACCOUNT_LOCKED_PAGE);
        GetLogoutUrlCallback callback = new GetLogoutUrlCallback(this.eventBus);
        this.gaeUserRpc.getLogoutUrl(destinationUrl, callback);
    }

    public ClientSession getSession() {
        return this.session;
    }

    public void setSession(ClientSession session) {
        this.session = session;
    }

    public IGaeUserRpcAsync getGaeUserRpc() {
        return this.gaeUserRpc;
    }

    public void setGaeUserRpc(IGaeUserRpcAsync gaeUserRpc) {
        this.gaeUserRpc = gaeUserRpc;
    }

    /** Callback for IGaeUserRpcAsync.getLoginUrl(). */
    protected static class GetLoginUrlCallback extends AsyncRpcCallback<String> {
        protected RootEventBus eventBus;

        public GetLoginUrlCallback(RootEventBus eventBus) {
            super("IGaeUserRpc", "getLoginUrl");
            this.eventBus = eventBus;
        }

        @Override
        public void onSuccessResult(String result) {
            this.eventBus.clearSession();  // clear it so it gets reloaded later
            WidgetUtils.getInstance().redirect(result);
        }
    }

    /** Callback for IGaeUserRpcAsync.getLogoutUrl(). */
    protected static class GetLogoutUrlCallback extends AsyncRpcCallback<String> {
        protected RootEventBus eventBus;

        public GetLogoutUrlCallback(RootEventBus eventBus) {
            super("IGaeUserRpc", "getLogoutUrl");
            this.eventBus = eventBus;
        }

        @Override
        public void onSuccessResult(String result) {
            this.eventBus.clearSession();  // clear it so it gets reloaded later
            WidgetUtils.getInstance().redirect(result);
        }
    }

}
